package lzupdater.gui;

import java.awt.Color;
import java.awt.Component;
import java.util.HashMap;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import lzupdater.log.LoggerLevel;
import lzupdater.log.LoggerListener;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Parent class a basic GUI logger for the application
 * 
 * @author Fraskor
 */
public abstract class AbstractLoggerPanel implements LoggerListener {

	private JScrollPane jScroll;
	private JTextPane pane;
	private Document document;
	private MutableAttributeSet defaultStyle;

	private HashMap<LoggerLevel, MutableAttributeSet> logStyle = new HashMap<LoggerLevel, MutableAttributeSet>();

	/**
	 * Constructor. Instantiate the GUI elements needed for the logger and
	 * initialise their properties
	 */
	public AbstractLoggerPanel() {
		pane = new JTextPane();
		document = pane.getDocument();
		jScroll = new JScrollPane(pane);
		jScroll
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		initializeGui();
	}

	/**
	 * @return Return the main gui component of the logger
	 */
	public Component getGui() {
		return jScroll;
	}

	/**
	 * {@inheritDoc}
	 */
	public void clear() {
		try {
			document.remove(0, document.getLength());
		} catch (BadLocationException e) {
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void displayMsg(String message, LoggerLevel level) {
		append(level.getHeader(), logStyle.get(level));
		append(message, defaultStyle);
		append("\n", defaultStyle);
	}

	/**
	 * {@inheritDoc}
	 */
	public abstract LoggerLevel getLoggerLevel();

	private void initializeGui() {
		pane.setEditable(false);
		pane.setBorder(new LineBorder(Color.black));
		pane.setBackground(new Color(0.9f, 0.9f, 0.9f));
		pane.setAutoscrolls(true);

		initialiseStyle();
	}

	private void initialiseStyle() {
		defaultStyle = createStyle(Color.black, false);
		logStyle.clear();

		logStyle.put(LoggerLevel.DEBUG, createStyle(Color.green, true));
		logStyle.put(LoggerLevel.INFO, createStyle(Color.black, true));
		logStyle.put(LoggerLevel.WARNING, createStyle(Color.orange, true));
		logStyle.put(LoggerLevel.ERROR, createStyle(Color.red, true));
		logStyle.put(LoggerLevel.ALL, createStyle(Color.gray, true));
	}

	private static SimpleAttributeSet createStyle(Color color, boolean bold) {
		SimpleAttributeSet s = new SimpleAttributeSet();
		StyleConstants.setForeground(s, color);
		StyleConstants.setBold(s, bold);
		StyleConstants.setFontFamily(s, "Courier");
		return s;
	}

	private void append(String msg, AttributeSet a) {
		try {
			document.insertString(document.getLength(), msg, a);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
}
